Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Django and HTMX I am looking for not "hacky" way to update class of link that calls HTMX request. Like "refresh-self" or something along those lines

I have a problem with HTMX in Django. I basically have two important components on page. List of categories and content that is being shown after you click on category.

I was working nicely with just standard htmx "out of the box". But I started having problems when I wanted to add active css class on category link after you click it (to show user where he is currently).

I did a lot of experiments with hx-swap-oob and hx-swap but the only thing that work was this:

(it is the most relevant part of the code)

<div class="col-sm-4">
    <div class="card">
        <div class="card-body" hx-boost="true" hx-target="#manual_results">
            <div id="manual_categories">
                {% include 'partials/manual_categories.html' %}
            </div>
        </div>
    </div>
</div>
<div class="col-sm-8">
    <div id="manual_results">
        {% include 'partials/manual_entries_list.html' %}
    </div>
</div>

and in manual_entries_list.html:

<some html results>
        <div id="manual_categories" hx-swap-oob="true">
            {% include 'partials/manual_categories.html' %}
        </div>

Each category has simple if statement in django template code that is checking if it is selected (based on url path.)

And it is working, thing is, on the first request the categories are rendered twice (which is logical since I have 2 includes on the same HTML). After I select one category, everything goes back to normal because HTMX "starts to understand what is happening" and it switches the categories from manual_entries_list.html into our main page.

And like I said it works, I modified manual_entries_list.html to:

    <some html results>
    <div class="set_size_to_0px">
        <div id="manual_categories" hx-swap-oob="true">
            {% include 'partials/manual_categories.html' %}
        </div>
    </div>

So it is always invisible (so I will have only one set of categories visible).

The thing is, that it is an obvious hack. I am sure that there needs to be a better way of solving this problem but I cannot find it.

(I even tried to do it with plain javascript the thing is that categories are rendered in a for loop so it is pretty much impossible to get IDs correctly etc.)

Could someone please help me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The easiest way to avoid this issue is to detect the HTMX request in the view function, pass this state to your templates and render HTMX content only if needed. HTMX will add a HX-Request: true header to each request.

For the detection you can use the Django-HTMX package that provides a request.htmx variable in your view functions that will be True if the request is coming from HTMX. Or if you want to check it manually:

def my_view(request):
    is_htmx = request.headers.get('HX-Request') == 'true'
    return render(request, 'my_template.html', {'is_htmx': is_htmx})

After that in manual_entries_list.html template include HTMX related stuff only in the HTMX requests:

<some html results>
{% if is_htmx %}
<div id="manual_categories" hx-swap-oob="true">
  {% include 'partials/manual_categories.html' %}
</div>
{% endif %}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!